package edu.ncsu.csc326.coffeemaker;

import edu.ncsu.csc326.coffeemaker.exceptions.InventoryException;
import junit.framework.TestCase;

/* Errors found:
 * 	make coffee crashes when out of bounds (problem with Main)
 * 	cannot reuse deleted entry
 *  cannot delete last element	(problem with Main)
 *  edit recipe erases name instead of keeping it
 */

/**
 * 
 * @author Sarah Heckman
 *
 * Modified by: Filipe Betzel
 *
 * Unit tests for CoffeeMaker class.
 */
public class CoffeeMakerTest extends TestCase {
	
	private CoffeeMaker cm;
	private Recipe r1;
	private Recipe r2;
	private Recipe r3;
	private Recipe r4;

	protected void setUp() throws Exception {
		cm = new CoffeeMaker();
		
		//Set up for r1
		r1 = new Recipe();
		r1.setName("Coffee");
		r1.setAmtChocolate("0");
		r1.setAmtCoffee("3");
		r1.setAmtMilk("1");
		r1.setAmtSugar("1");
		r1.setPrice("50");
		
		//Set up for r2
		r2 = new Recipe();
		r2.setName("Mocha");
		r2.setAmtChocolate("20");
		r2.setAmtCoffee("3");
		r2.setAmtMilk("1");
		r2.setAmtSugar("1");
		r2.setPrice("75");
		
		//Set up for r3
		r3 = new Recipe();
		r3.setName("Latte");
		r3.setAmtChocolate("0");
		r3.setAmtCoffee("3");
		r3.setAmtMilk("3");
		r3.setAmtSugar("1");
		r3.setPrice("100");
		
		//Set up for r4
		r4 = new Recipe();
		r4.setName("Hot Chocolate");
		r4.setAmtChocolate("4");
		r4.setAmtCoffee("0");
		r4.setAmtMilk("1");
		r4.setAmtSugar("1");
		r4.setPrice("65");
		
		super.setUp();
	}
	
	/////////////////////
	//addRecipe(Recipe)
	/////////////////////
	
	public void testAddRecipe() {	
		assertTrue(cm.addRecipe(r1));
	}
	
	// trying to add the same recipe twice should fail
	public void testAddRepeatedRecipe() {
		cm.addRecipe(r1);
		assertFalse(cm.addRecipe(r1));
	}
	
	public void testAdd4Recipes() {
		assertTrue(cm.addRecipe(r1));
		assertTrue(cm.addRecipe(r2));
		assertTrue(cm.addRecipe(r3));
		assertTrue(cm.addRecipe(r4));
	}
	
	public void testAddAndDelete() {
		assertTrue(cm.addRecipe(r1));
		assertEquals(cm.deleteRecipe(0), r1.getName());
		assertTrue(cm.addRecipe(r1));
		assertTrue(cm.addRecipe(r2));
		assertTrue(cm.addRecipe(r3));
		assertTrue(cm.addRecipe(r4));
	}
	
	
	/////////////////////
	//deleteRecipe(int)
	/////////////////////
	
	public void testDeleteARecipeR1() {	
		cm.addRecipe(r1);
		assertEquals(cm.deleteRecipe(0), r1.getName());
	}
	
	public void testDeleteLastRecipe() {
		cm.addRecipe(r1);
		cm.addRecipe(r2);
		cm.addRecipe(r3);
		cm.addRecipe(r4);
		assertEquals(cm.deleteRecipe(3), r4.getName());
	}
	
	/////////////////////
	//editRecipe(int, Recipe)
	/////////////////////
	
	public void testEditARecipeR1() {		
		cm.addRecipe(r1);
		assertEquals(cm.editRecipe(0, r1), r1.getName());
	}
	
	/////////////////////
	//addInventory(String, String, String, String)
	/////////////////////
	
	public void testAddInventory() {
		try {
			cm.addInventory("4","7","0","9");
		} catch (InventoryException e) {
			fail("InventoryException should not be thrown");
		}
	}
	
	public void testAddInventoryException() {
		try {
			cm.addInventory("4", "-1", "asdf", "3");
			fail("InventoryException should be thrown");
		} catch (InventoryException e) {
			//success if thrown
		}
	}
	
	
	/////////////////////
	//checkInventory()
	/////////////////////
	
	public void testCheckInventory() {		
		cm.checkInventory();
	}
	
	/////////////////////
	//makeCoffee(int, int)
	/////////////////////

	public void testMakeCoffee() {
		cm.addRecipe(r1);
		assertEquals(25, cm.makeCoffee(0, 75));
	}
	
	public void testMakeCoffeeNotEnoughMoney() {
		cm.addRecipe(r1);
		assertEquals(30, cm.makeCoffee(0, 30));
	}
	
	public void testMakeCoffeeInvalidRecipe() {
		cm.addRecipe(r1);
		assertEquals(75, cm.makeCoffee(1, 75));
	}
	
	public void testMakeCoffeeOutOfChocolate() {
		cm.addRecipe(r4);
		cm.makeCoffee(0, 75);
		cm.makeCoffee(0, 75);
		cm.makeCoffee(0, 75);
		assertEquals(75, cm.makeCoffee(0, 75));
	}
	
	/////////////////////
	//getRecipes()
	/////////////////////
	
	public void testGetRecipes() {
		cm.getRecipes();
	}

}
